#ifndef CAMERADAEMON_H
#define CAMERADAEMON_H

#include <semaphore.h>
#include <pthread.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <string>

#include "timestamp.h"
class CameraDaemon
{
public:
    CameraDaemon();
    ~CameraDaemon();
    bool startCapture(int device = -1, int fps = 30);
    bool startCapture(std::string file, int fps = 30);
    bool getImage(cv::Mat& img, TimeStamp& t);
    void stopCapture();
    friend void* captureThread(void* _cam);
    void setFocus(int f);
    void setBrightness(int f);
    void setContrast(int c);
    void setWidth(int w);
    void setHeight(int h);
    void startRecord();
    void stopRecord();
    int getFocus(){
        return m_focus;
    }
    int getBrightness(){
        return m_brightness;
    }
    int getContrast(){
        return m_contrast;
    }
    int getWidth(){
        return m_width;
    }
    int getHeight(){
        return m_height;
    }

private:
    bool m_from_file;
    int m_fps;
    int m_interval;
    std::string m_file;
    int m_device;
    void capture();
    pthread_t m_capture_thread;
    bool m_working;
    cv::VideoCapture m_capture;
    sem_t m_buffer_mutex;
    TimeStamp m_buffer_time;
    cv::Mat m_buffer;

#define RECORD_FOLDER "./record/"
    bool m_recording;
    std::string m_record_file;
    cv::VideoWriter m_record_writer;
    int m_width;
    int m_height;
    int m_brightness;
    int m_contrast;
    int m_focus;

};

#endif // CAMERADAEMON_H
